


Namespace xRtl
	Namespace Charset
		
		Function A2W(ZStrPtr As ZString Ptr, ZStrLen As UInteger = 0) As Any Ptr
			If ZStrPtr Then
				If ZStrLen = 0 Then
					ZStrLen = strlen(ZStrPtr)
				EndIf
				Dim WStrLen As Integer = MultiByteToWideChar(CP_ACP, 0, ZStrPtr, ZStrLen, NULL, 0)
				Dim WStrMem As WString Ptr = xRtl.TempMemory((WStrLen + 1) * SizeOf(WString))
				MultiByteToWideChar(CP_ACP, 0, ZStrPtr, ZStrLen, WStrMem, WStrLen)
				WStrMem[WStrLen] = 0
				Return WStrMem
			EndIf
		End Function
		
		Function W2A(WStrPtr As WString Ptr, WStrLen As UInteger = 0) As Any Ptr
			If WStrPtr Then
				If WStrLen = 0 Then
					WStrLen = wcslen(WStrPtr)
				EndIf
				Dim ZStrLen As Integer = WideCharToMultiByte(CP_ACP, 0, WStrPtr, WStrLen, NULL, 0, NULL, NULL)
				Dim ZStrMem As ZString Ptr = xRtl.TempMemory(ZStrLen + 1)
				WideCharToMultiByte(CP_ACP, 0, WStrPtr, WStrLen, ZStrMem, ZStrLen, NULL, NULL)
				ZStrMem[ZStrLen] = 0
				Return ZStrMem
			EndIf
		End Function
		
		Function W2U(WStrPtr As WString Ptr, WStrLen As UInteger = 0) As Any Ptr
			If WStrPtr Then
				If WStrLen = 0 Then
					WStrLen = wcslen(WStrPtr)
				EndIf
				Dim UTF8Len As Integer = WideCharToMultiByte(CP_UTF8, 0, WStrPtr, WStrLen, NULL, 0, NULL, NULL)
				Dim UTF8Mem As ZString Ptr = xRtl.TempMemory(UTF8Len + 1)
				WideCharToMultiByte(CP_UTF8, 0, WStrPtr, WStrLen, UTF8Mem, UTF8Len, NULL, NULL)
				UTF8Mem[UTF8Len] = 0
				Return UTF8Mem
			EndIf
		End Function
		
		Function U2W(UTF8Ptr As ZString Ptr,UTF8Len As UInteger = 0) As Any Ptr
			If UTF8Ptr Then
				If UTF8Len = 0 Then
					UTF8Len = strlen(UTF8Ptr)
				EndIf
				Dim WStrLen As Integer = MultiByteToWideChar(CP_UTF8, 0, UTF8Ptr, UTF8Len, NULL, 0)
				Dim WStrMem As WString Ptr = xRtl.TempMemory((WStrLen + 1) * SizeOf(WString))
				MultiByteToWideChar(CP_UTF8, 0, UTF8Ptr, UTF8Len, WStrMem, WStrLen)
				WStrMem[WStrLen] = 0
				Return WStrMem
			EndIf
		End Function
		
		Function A2U(ZStr As ZString Ptr, ZLen As UInteger = 0) As ZString Ptr
			If ZStr Then
				Dim TempMem As Any Ptr = A2W(ZStr, ZLen)
				Return W2U(TempMem, 0)
			EndIf
		End Function
		
		Function U2A(UStr As ZString Ptr,ULen As UInteger = 0) As ZString Ptr
			If UStr Then
				Dim TempMem As Any Ptr = U2W(UStr, ULen)
				Return W2A(TempMem, 0)
			EndIf
		End Function
		
		Function A2W_C(ZStrPtr As ZString Ptr, ZStrLen As UInteger = 0) As Any Ptr
			If ZStrPtr Then
				If ZStrLen = 0 Then
					ZStrLen = strlen(ZStrPtr)
				EndIf
				Dim WStrLen As Integer = MultiByteToWideChar(CP_ACP, 0, ZStrPtr, ZStrLen, NULL, 0)
				Dim WStrMem As WString Ptr = Allocate((WStrLen + 1) * SizeOf(WString))
				MultiByteToWideChar(CP_ACP, 0, ZStrPtr, ZStrLen, WStrMem, WStrLen)
				WStrMem[WStrLen] = 0
				Return WStrMem
			EndIf
		End Function
		
		Function W2A_C(WStrPtr As WString Ptr, WStrLen As UInteger = 0) As Any Ptr
			If WStrPtr Then
				If WStrLen = 0 Then
					WStrLen = wcslen(WStrPtr)
				EndIf
				Dim ZStrLen As Integer = WideCharToMultiByte(CP_ACP, 0, WStrPtr, WStrLen, NULL, 0, NULL, NULL)
				Dim ZStrMem As ZString Ptr = Allocate(ZStrLen + 1)
				WideCharToMultiByte(CP_ACP, 0, WStrPtr, WStrLen, ZStrMem, ZStrLen, NULL, NULL)
				ZStrMem[ZStrLen] = 0
				Return ZStrMem
			EndIf
		End Function
		
		Function W2U_C(WStrPtr As WString Ptr, WStrLen As UInteger = 0) As Any Ptr
			If WStrPtr Then
				If WStrLen = 0 Then
					WStrLen = wcslen(WStrPtr)
				EndIf
				Dim UTF8Len As Integer = WideCharToMultiByte(CP_UTF8, 0, WStrPtr, WStrLen, NULL, 0, NULL, NULL)
				Dim UTF8Mem As ZString Ptr = Allocate(UTF8Len + 1)
				WideCharToMultiByte(CP_UTF8, 0, WStrPtr, WStrLen, UTF8Mem, UTF8Len, NULL, NULL)
				UTF8Mem[UTF8Len] = 0
				Return UTF8Mem
			EndIf
		End Function
		
		Function U2W_C(UTF8Ptr As ZString Ptr,UTF8Len As UInteger = 0) As Any Ptr
			If UTF8Ptr Then
				If UTF8Len = 0 Then
					UTF8Len = strlen(UTF8Ptr)
				EndIf
				Dim WStrLen As Integer = MultiByteToWideChar(CP_UTF8, 0, UTF8Ptr, UTF8Len, NULL, 0)
				Dim WStrMem As WString Ptr = Allocate((WStrLen + 1) * SizeOf(WString))
				MultiByteToWideChar(CP_UTF8, 0, UTF8Ptr, UTF8Len, WStrMem, WStrLen)
				WStrMem[WStrLen] = 0
				Return WStrMem
			EndIf
		End Function
		
		Function A2U_C(ZStr As ZString Ptr, ZLen As UInteger = 0) As ZString Ptr
			If ZStr Then
				Dim TempMem As Any Ptr = A2W(ZStr, ZLen)
				Return W2U_C(TempMem, 0)
			EndIf
		End Function
		
		Function U2A_C(UStr As ZString Ptr,ULen As UInteger = 0) As ZString Ptr
			If UStr Then
				Dim TempMem As Any Ptr = U2W(UStr, ULen)
				Return W2A_C(TempMem, 0)
			EndIf
		End Function
		
	End Namespace
End Namespace
